﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace CJJCH.Web.Manage
{
    public partial class NewsEdit : PowerBase
    {
        CJJCH.BLL.T_NET_News bll_news = new CJJCH.BLL.T_NET_News();
        CJJCH.BLL.T_NET_MenuSecond bll_menuSecond = new CJJCH.BLL.T_NET_MenuSecond();
        CJJCH.BLL.T_NET_MenuThird bll_menuThird = new CJJCH.BLL.T_NET_MenuThird();
        protected void Page_Load(object sender, EventArgs e)
        {
            this.form1.DefaultButton = "btnSave";
            if (!IsPostBack)
            {
                InitControl();
                InitRequest();
                ShowNews();
            }
            LTP.Common.ButtonClass.ButtonOnClick(this.Page, btnSave, "checkform()", false);
        }

        private void InitControl()
        {
            if (Request.QueryString["secondid"] != null && LTP.Common.CheckInput.CheckDigit(Request.QueryString["secondid"].Trim()))
            {
                CJJCH.Model.T_NET_MenuSecond model = new CJJCH.Model.T_NET_MenuSecond();
                model = bll_menuSecond.GetMenuSecondModel(Convert.ToInt32(Request.QueryString["secondid"].ToString()));
                if (model != null)
                {
                    this.lblSecondID.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["secondid"].Trim());
                    this.lblSecondName.Text = model.MenuName;
                    this.lblFirstID.Text = model.FirstID.ToString();
                    if (!model.HaveChild)
                    {
                        this.tdType.Style.Add("display", "none;");
                    }
                    else
                    {
                        IList<CJJCH.Model.T_NET_MenuThird> list = new List<CJJCH.Model.T_NET_MenuThird>();
                        list = bll_menuThird.GetDrop(Convert.ToInt32(this.lblSecondID.Text.Trim()));
                        int typeCount = list.Count;
                        if (typeCount > 0)
                        {
                            for (int i = 0; i < typeCount; i++)
                            {
                                dropNewsType.Items.Add(new ListItem(list[i].MenuName, list[i].ID.ToString()));
                            }
                            list = null;
                        }
                    }
                }
                else
                {
                    LTP.Common.WindowClass.WindowBack("操作错误！");
                }
            }
            else
            {
                LTP.Common.WindowClass.WindowBack("操作错误！");
            }           
        }

        private void InitRequest()
        {
            if (Request.QueryString["secondid"] != null && LTP.Common.CheckInput.CheckDigit(Request.QueryString["secondid"].ToString()))
            {
                this.lblSecondID.Text = Request.QueryString["secondid"].ToString();
            }
            if (Request.QueryString["secondname"] != null)
            {
                this.lblSecondName.Text = Request.QueryString["secondname"].ToString();
            }
            if (Request.QueryString["keyword"] != null)
            {
                lblKeyword.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["keyword"].Trim());
            }
            if (Request.QueryString["orderkey"] != null)
            {
                lblOrderKey.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["orderkey"].Trim());
            }
            if (Request.QueryString["ordertype"] != null && LTP.Common.CheckInput.CheckDigit(Request.QueryString["ordertype"].Trim()))
            {
                lblOrderType.Text = Request.QueryString["ordertype"].Trim();
            }
            if (Request.QueryString["thirdid"] != null && LTP.Common.CheckInput.CheckDigit(Request.QueryString["thirdid"].Trim()))
            {
                lblThirdID.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["thirdid"].Trim());
            }
            if (Request.QueryString["betime"] != null && LTP.Common.CheckInput.CheckDateTime(Request.QueryString["betime"].Trim()))
            {
                lblBeTime.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["betime"].Trim());
            }
            if (Request.QueryString["edtime"] != null && LTP.Common.CheckInput.CheckDateTime(Request.QueryString["edtime"].Trim()))
            {
                lblEdTime.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["edtime"].Trim());
            }
            if (Request.QueryString["curpage"] != null && LTP.Common.CheckInput.CheckDigit(Request.QueryString["curpage"].ToString()))
            {
                this.lblCurPage.Text = Request.QueryString["curpage"].ToString();
            }
            if (Request.QueryString["ID"] != null && LTP.Common.CheckInput.CheckGUID(Request.QueryString["ID"].ToString().Trim()))
            {
                ViewState["ID"] = Request.QueryString["ID"].ToString().Trim();
            }
            else
            {
                LTP.Common.WindowClass.WindowBack("操作错误！");
            }
            this.lnkNews.NavigateUrl = "NewsList.aspx?secondid="
                    + LTP.Common.StringClass.EncodeUrl(this.lblSecondID.Text.Trim())
                    + "&keyword=" + LTP.Common.StringClass.EncodeUrl(this.lblKeyword.Text.Trim())
                    + "&orderkey=" + LTP.Common.StringClass.EncodeUrl(this.lblOrderKey.Text.Trim())
                    + "&ordertype=" + LTP.Common.StringClass.EncodeUrl(this.lblOrderType.Text.Trim())
                    + "&thirdid=" + LTP.Common.StringClass.EncodeUrl(this.lblThirdID.Text.Trim())
                    + "&betime=" + LTP.Common.StringClass.EncodeUrl(this.lblBeTime.Text.Trim())
                    + "&edtime=" + LTP.Common.StringClass.EncodeUrl(this.lblEdTime.Text.Trim())
                    + "&curpage=" + LTP.Common.StringClass.EncodeUrl(this.lblCurPage.Text.Trim());
            this.lnkNews.Text = lblSecondName.Text.Trim() + "列表";
            this.lblNews.Text = lblSecondName.Text.Trim() + "修改";
        }

        private void ShowNews()
        {
            CJJCH.Model.T_NET_News model_news = new CJJCH.Model.T_NET_News();
            model_news = bll_news.GetNewsModel(new Guid(ViewState["ID"].ToString()));
            if (model_news != null)
            {
                this.txtAuthor.Text = model_news.DataAuthor;
                this.txtNote.InnerHtml = model_news.DataNote;
                this.txtOrderNum.Text = model_news.OrderNum.ToString();
                this.txtPlusDate.Text = model_news.PlusDate.ToString("yyyy/MM/dd");
                this.txtSource.Text = model_news.DataSource;
                this.txtTitle.Text = model_news.DataTitle;
                if (tdType.Style["display"] != "none")
                {
                    this.dropNewsType.SelectedValue = model_news.ThirdID.ToString();
                }              
                this.chkIsPic.Checked = model_news.IsPic;
                this.lblPicUrl.Text = model_news.PicUrl;
                this.chkIsHot.Checked = model_news.IsHot;
                this.chkIsTop.Checked = model_news.IsTop;
            }
        }
        
        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("NewsList.aspx?secondid="
                    + LTP.Common.StringClass.EncodeUrl(this.lblSecondID.Text.Trim())
                    + "&keyword=" + LTP.Common.StringClass.EncodeUrl(this.lblKeyword.Text.Trim())
                    + "&orderkey=" + LTP.Common.StringClass.EncodeUrl(this.lblOrderKey.Text.Trim())
                    + "&ordertype=" + LTP.Common.StringClass.EncodeUrl(this.lblOrderType.Text.Trim())
                    + "&thirdid=" + LTP.Common.StringClass.EncodeUrl(this.lblThirdID.Text.Trim())
                    + "&betime=" + LTP.Common.StringClass.EncodeUrl(this.lblBeTime.Text.Trim())
                    + "&edtime=" + LTP.Common.StringClass.EncodeUrl(this.lblEdTime.Text.Trim())
                    + "&curpage=" + LTP.Common.StringClass.EncodeUrl(this.lblCurPage.Text.Trim()));
        }

        protected string checkInput()
        {
            StringBuilder error = new StringBuilder();
            if (string.IsNullOrEmpty(this.txtTitle.Text.Trim()))
            {
                error.Append("请输入标题！ \\n");
            }
            if (string.IsNullOrEmpty(this.txtAuthor.Text.Trim()))
            {
                error.Append("请输入作者！ \\n");
            }
            if (string.IsNullOrEmpty(this.txtSource.Text.Trim()))
            {
                error.Append("请输入来源！ \\n");
            }
            if (this.tdType.Style["display"] != "none")
            {
                if (string.IsNullOrEmpty(this.dropNewsType.SelectedValue))
                {
                    error.Append("请选择所属类别！ \\n");
                }
                else if (!LTP.Common.CheckInput.CheckDigit(this.dropNewsType.SelectedValue))
                {
                    error.Append(" 所属类别选择有误！ \\n ");
                }
            }
            if (string.IsNullOrEmpty(this.txtPlusDate.Text.Trim()))
            {
                error.Append("请选择发布日期！ \\n");
            }
            else if (!LTP.Common.CheckInput.CheckDateTime(this.txtPlusDate.Text.Trim()))
            {
                error.Append("请选择正确的日期！ \\n");
            }
            if (string.IsNullOrEmpty(this.txtNote.InnerText.Trim()))
            {
                error.Append("请输入内容！ \\n");
            }
            if (String.IsNullOrEmpty(this.txtOrderNum.Text.Trim()))
            {
                error.Append("请输入显示排序号！ \\n");
            }
            else if (!LTP.Common.CheckInput.CheckDigit(this.txtOrderNum.Text.Trim()))
            {
                error.Append("显示排序请输入整数！ \\n");
            }
            return error.ToString();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string error = checkInput();
            if (error == "")
            {
                CJJCH.Model.T_NET_News model = new Model.T_NET_News();
                model.ID = new Guid(ViewState["ID"].ToString());
                model.DataAuthor = LTP.Common.StringClass.EncodeString(this.txtAuthor.Text.Trim());
                model.DataNote = LTP.Common.StringClass.EncodeStr(this.txtNote.InnerText.Trim());
                model.DataSource = LTP.Common.StringClass.EncodeString(this.txtSource.Text.Trim());
                model.DataTitle = LTP.Common.StringClass.EncodeString(this.txtTitle.Text.Trim());
                model.FirstID = Convert.ToInt32(this.lblFirstID.Text.Trim());
                model.IsHot = Convert.ToBoolean(this.chkIsHot.Checked);
                model.IsTop = Convert.ToBoolean(this.chkIsTop.Checked);
                model.ManagerID = SessionManagerID;
                model.OrderNum = Convert.ToInt32(this.txtOrderNum.Text.Trim());
                if (!string.IsNullOrEmpty(this.txtPlusDate.Text.Trim()) && LTP.Common.CheckInput.CheckDateTime(this.txtPlusDate.Text.Trim()))
                {
                    model.PlusDate = Convert.ToDateTime(this.txtPlusDate.Text.Trim());
                }
                model.SecondID = Convert.ToInt32(this.lblSecondID.Text.Trim());
                if (tdType.Style["display"] != "none")
                {
                    model.ThirdID = Convert.ToInt32(this.dropNewsType.SelectedValue);
                }
                else
                {
                    model.ThirdID = 0;
                }
                model.BuildDate = DateTime.Now;
                if (bll_news.UpdateNews(model) == "")
                {
                    Response.Redirect("NewsList.aspx?secondid="
                        + LTP.Common.StringClass.EncodeUrl(lblSecondID.Text.Trim())
                        + "&keyword=" + LTP.Common.StringClass.EncodeUrl(lblKeyword.Text.Trim())
                        + "&orderkey=" + LTP.Common.StringClass.EncodeUrl(lblOrderKey.Text.Trim())
                        + "&ordertype=" + LTP.Common.StringClass.EncodeUrl(lblOrderType.Text.Trim())
                        + "&thirdid=" + LTP.Common.StringClass.EncodeUrl(lblThirdID.Text.Trim())
                        + "&betime=" + LTP.Common.StringClass.EncodeUrl(lblBeTime.Text.Trim())
                        + "&edtime=" + LTP.Common.StringClass.EncodeUrl(lblEdTime.Text.Trim())
                        + "&curpage=" + LTP.Common.StringClass.EncodeUrl(this.lblCurPage.Text.Trim()));
                }
                else
                {
                    LTP.Common.WindowClass.WindowBack("更新失败！");
                }
            }
            else 
            {
                LTP.Common.WindowClass.WindowBack(error);
            }
        }


    }
}